﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Quan_ly_thong_tin_he_thong
{
    public partial class ThemDonMuaHang : Form
    {
        public ThemDonMuaHang()
        {
            InitializeComponent();
        }

        private void Them_DonMuaHang_Load(object sender, EventArgs e)
        {
            Load_MaSo_NCU();
        }

        private void Load_MaSo_NCU()
        {
            Config file = new Config(Application.StartupPath + "\\Config.ini");
            string connectionString = file.ReadValue("server", "con");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from NhaCungUng";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "NhaCungUng");
            DataTable dt = ds.Tables["NhaCungUng"];

            comboBoxMaSo_NCU.DisplayMember = "DiaChi_NCU";
            comboBoxMaSo_NCU.ValueMember = "MaSo_NCU";
            comboBoxMaSo_NCU.DataSource = ds.Tables["NhaCungUng"].DefaultView;
        }

        private void ButttonThemDMH_Click(object sender, EventArgs e)
        {
            labelThongBaoThemDMH.Text = "";
            if (textBoxMaSo_DMH.Text == "" || dateTimePickerNgayMuaHang.Text == "")
                labelThongBaoThemDMH.Text = "Xin nhập đầy đủ dữ liệu!";
            else
            {
                Config file = new Config(Application.StartupPath + "\\Config.ini");
                string connectionString = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "select * from DonMuaHang where MaSo_DMH = @iddmh";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@iddmh", textBoxMaSo_DMH.Text);

                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    labelThongBaoThemDMH.Text = "Mã số đơn mua hàng đã tồn tại!";
                    reader.Close();
                }
                else
                {
                    reader.Close();
                    if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                    {
                        labelThongBaoThemDMH.Text = "Đã thêm dữ liệu thành công";
                        this.DialogResult = DialogResult.OK;

                        cmd.Parameters.Clear();
                        cmd.CommandText = "insert into DonMuaHang(MaSo_DMH, NgayMuaHang, MaSo_NCU, XacNhan_DMH) values (@iddmh, @ngmh, @idncu, @xn)";
                        cmd.Parameters.AddWithValue("@iddmh", textBoxMaSo_DMH.Text);
                        cmd.Parameters.AddWithValue("@ngmh", dateTimePickerNgayMuaHang.Value);
                        cmd.Parameters.AddWithValue("@idncu", comboBoxMaSo_NCU.SelectedValue);
                        cmd.Parameters.AddWithValue("@xn", textBoxXacNhan.Text);
                        cmd.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }
        }

        private void buttonThoatDMH_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
